<?php

namespace app\common\model;

use think\Model;

class FundsType extends Model
{
    // 表名
    protected $name = 'funds_type';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    
    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text',
	    'type_text'
    ];
    

    
    const TYPE_LESS = 1;
    const TYPE_PUSH = 2;
    
	public static $typeList = [
		self::TYPE_LESS => '支出',
		self::TYPE_PUSH => '收入'
	];
	
	public function getTypeTextAttr($value, $data) {
		$value = $value ? $value : $data['type'];
		$list = self::$typeList;
		return isset($list[$value]) ? $list[$value] : '';
	}


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['update_time']) ? $data['update_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

	public function category(){
		return $this->belongsTo('fundsCategory', 'funds_category_id', 'id', '', 'left')->setEagerlyType(0);
	}
}
